home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2890 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.0 KB

  1. Path: paranoia.com!avinash
  2. From: avinash@paranoia.com (Avinash Chopde)
  3. Newsgroups: comp.lang.c
  4. Subject: Fastest way to computer log(base2) of x?
  5. Date: 24 Jan 1996 19:33:49 GMT
  6. Organization: None.
  7. Message-ID: <4e61iu$p6e@villa.fc.net>
  8. NNTP-Posting-Host: primus.paranoia.com
  9.  
  10. [And no, this is not for a homework exercise.]
  11. [BTW, the non-abridged C FAW at http://www.eskimo.com/~scs/C-faq.top.html
  12. does not seem to be accessible??]
  13.  
  14. I am trying to find out what could be the fastest way to compute
  15. the position of the highest bit in any given integer -- basically, the
  16. logarithm to the base 2 of any number.
  17.  
  18. Simplistically, one could do :
  19. logbase2(int x)
  20. {
  21.     if (IS_SET_BIT_31(x)) return 31;
  22.     if (IS_SET_BIT_30(x)) return 30;
  23.     etc.
  24. }
  25.  
  26. An improvement would be to check for BITS_31_16 and BITS_15_0 at first, and
  27. then check for BITS_31_24, and BITS_23_16, etc .. (divide problem in half
  28. at each stage).
  29.  
  30. Any thoughts or other ideas?
  31.  
  32. Thanks!
  33.  
  34. -- 
  35. ----
  36. Avinash Chopde
  37. e-mail: avinash@acm.org
  38. home page: http://www.paranoia.com/~avinash/
  39.